package com.ddyx.service.product.app.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddyx.service.product.api.entity.Goods;
import com.ddyx.service.product.api.vo.GoodsVo;
import com.ddyx.service.product.app.system.mapper.GoodsMapper;
import com.ddyx.service.product.app.system.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author agula
 * @since 2019-08-06
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public void add(GoodsVo goodsVo) {
        /*对象转换*/
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        MapperFacade mapper = mapperFactory.getMapperFacade();

        Goods goods = mapper.map(goodsVo, Goods.class);
        save(goods);
        log.info("商品添加完成");
    }

    @Override
    public List<Goods> getList() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByAsc("create_time");
        List<Goods> goodsList = goodsMapper.selectList(wrapper);
        log.info("商品列表查询完成");
        return goodsList;
    }
}
